package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/8 12:03
 * @description：https://leetcode-cn.com/problems/gray-code/
 */
public class 格雷编码 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.grayCode(3));
    }

    class Solution {
        public List<Integer> grayCode(int n) {
            List<Integer> ans = new ArrayList<>();
            ans.add(0);
            for (int i = 1; i <= n; i++) {
                int m = ans.size();
                for (int j = m - 1; j >= 0; j--) {
                    ans.add(ans.get(j) | (1 << (i - 1)));
                }
            }
            return ans;
        }
    }
}

